package portlet;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import org.apache.myfaces.trinidad.component.UIXIterator;

import portlet.resource.chitietqdcbBundle;

public class chitietqdcb extends ADFBridgePortlet {
    private String qdid;
    private String langid;
    private String urlquyetdinh;
    private String urldownload;
    private String tieude;
    private String noidung;
    private boolean checkcount = false;
    private Object filedinhkem;
    private boolean hasFile = false;
    private UIXIterator qdcbIter = new UIXIterator();
    //Labels
    private String labelPortlet;
    private String labelQuyetdinhkhac;
    private String labelFiledinhkem;

    public String getViewRender() {
        getLangid();
        getQdid();
        getQuyetdinhCongbo();
        return null;
    }

    private void getQuyetdinhCongbo() {
        DCIteratorBinding qdcbIterator =
            Utils.getIteratorBinding("TthcQdcbView1Iterator");
        ViewObject vo = qdcbIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("qdcbViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        if (qdid != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("Id");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem2.setValue(qdid);
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("qdcbViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            tieude = vo.getCurrentRow().getAttribute("Tieude").toString();
            noidung = vo.getCurrentRow().getAttribute("Noidung").toString();
            filedinhkem = vo.getCurrentRow().getAttribute("FileId");
            getHtFileObject();
            getQuyetdinhCongboKhac();
        }
    }

    private void getQuyetdinhCongboKhac() {
        DCIteratorBinding qdcbIterator =
            Utils.getIteratorBinding("TthcQdcbView1Iterator");
        ViewObject vo = qdcbIterator.getViewObject();
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("qdcbViewCriteria");
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("qdcbViewCriteria1");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
        ViewCriteriaItem rowViewCriteriaItem2 = vcRow.ensureCriteriaItem("Id");
        rowViewCriteriaItem2.setOperator(JboCompOper.OPER_NE);
        rowViewCriteriaItem2.setValue(qdid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("qdcbViewCriteria1");
        vo.applyViewCriteria(vc);
        vo.setRangeSize(10);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            checkcount = true;
        }
        Utils.iteratorRefresh("TthcQdcbView1Iterator");
    }

    public void getHtFileObject() {
        DCIteratorBinding fileIterator = Utils.getIteratorBinding("HtFileView1Iterator");
        ViewObject vo = fileIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "IN (0)";
        if (getFiledinhkem() != null) {
            hasFile = true;
            varStr = "IN (" + getFiledinhkem() + ")";
        }
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();

    }

    public String getQdid() {
        try {
            qdid = Utils.getPortletRequest().getParameter("id");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qdid;
    }

    public String getLangid() {
        try {
            langid = Utils.getPortalLocale().getLanguage();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return langid;
    }

    public String getUrlquyetdinh() {
        try {
            urlquyetdinh =
                    Utils.getPortletRequest().getParameter("urlquyetdinh");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlquyetdinh;
    }


    public void setCheckcount(boolean checkcount) {
        this.checkcount = checkcount;
    }

    public boolean isCheckcount() {
        return checkcount;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public String getTieude() {
        return tieude;
    }

    public void setNoidung(String noidung) {
        this.noidung = noidung;
    }

    public String getNoidung() {
        return noidung;
    }


    public String getLabelPortlet() {
        try {
            labelPortlet =
                    Utils.getPortletRequest().getParameter("tieudeportlet");
            if (labelPortlet == null) {
                labelPortlet = Utils.getResource("QUYET_DINH_CONG_BO");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return labelPortlet;
    }

    public String getLabelQuyetdinhkhac() {
        labelQuyetdinhkhac = Utils.getResource("QUYET_DINH_CONG_BO_KHAC");
        return labelQuyetdinhkhac;
    }

    public void setQdcbIter(UIXIterator qdcbIter) {
        this.qdcbIter = qdcbIter;
    }

    public UIXIterator getQdcbIter() {
        return qdcbIter;
    }

    public String getUrldownload() {
        try {
            urldownload =
                    Utils.getPortletRequest().getParameter("urldownload");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldownload;
    }

    public void setFiledinhkem(Object filedinhkem) {
        this.filedinhkem = filedinhkem;
    }

    public Object getFiledinhkem() {
        return filedinhkem;
    }

    public void setHasFile(boolean hasFile) {
        this.hasFile = hasFile;
    }

    public boolean isHasFile() {
        return hasFile;
    }

    public String getLabelFiledinhkem() {
        labelFiledinhkem = Utils.getResource("TAP_TIN_DINH_KEM");
        return labelFiledinhkem;
    }
}
